package uit.shoeshop.dao;
import uit.shoeshop.dto.SlideshowDTO;
import uit.shoeshop.lib.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;


public class SlideshowDAO {
	private Connection conn;
	private String tableName;
	
	public SlideshowDAO(){
		setConn(null);
		setTableName(Constants.TBL_SLIDESHOW);
	}

	public ArrayList<SlideshowDTO> getSlideshow() throws SQLException{
		ArrayList<SlideshowDTO> results = new ArrayList<SlideshowDTO>();
		
		try{
			setConn(Util.Connect());
			String sql = "SELECT * FROM " + getTableName() + " ORDER BY create_date DESC";
			PreparedStatement stm = getConn().prepareStatement(sql);
			
			//thuc hien cau lenh sql
			ResultSet rs = stm.executeQuery();
			
			while (rs.next()) {
				SlideshowDTO slideshow = new SlideshowDTO();
				slideshow.setSlideshow_id(rs.getInt("slideshow_id"));
				slideshow.setTitle(rs.getString("title"));
				slideshow.setContent(rs.getString("content"));
				slideshow.setImage(rs.getString("image"));
				results.add(slideshow);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(getConn() != null)
				getConn().close();
		}
		return results;
	}
	
	public SlideshowDTO getSlideshowById(int sId) throws SQLException{
		SlideshowDTO results = new SlideshowDTO();
		
		try{
			setConn(Util.Connect());
			String sql = "SELECT * FROM " + getTableName() + " WHERE slideshow_id = ?";
			PreparedStatement stm = getConn().prepareStatement(sql);
			stm.setInt(1, sId);
			//thuc hien cau lenh sql
			ResultSet rs = stm.executeQuery();
			
			while (rs.next()) {
				results.setSlideshow_id(rs.getInt("slideshow_id"));
				results.setTitle(rs.getString("title"));
				results.setContent(rs.getString("content"));
				results.setImage(rs.getString("image"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(getConn() != null)
				getConn().close();
		}
		return results;
	}
	
	public int delSlideshow(int sId) throws SQLException{
		int result = 1;
		try{
			setConn(Util.Connect());
			String sql = "DELETE FROM " +getTableName() +" WHERE slideshow_id = ?";
			PreparedStatement stm = getConn().prepareStatement(sql);
			stm.setInt(1, sId);
			result = stm.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
			result = 0;
		}finally{
			if(getConn() != null)
				getConn().close();
		}		
		return result;
	}	

	public int updateSlideshowById(int sId,String title,String content,String image) throws SQLException{
		int result = 1;
		try{
			setConn(Util.Connect());
			String sql = "";
			sql = "UPDATE "+getTableName()+" SET title=?,content=?,image=? WHERE slideshow_id=?";			

			PreparedStatement stm = getConn().prepareStatement(sql);
			
			//gan gia tri cho cau sql
			stm.setString(1, title);
			stm.setString(2, content);
			stm.setString(3, image);			
			stm.setInt(4, sId);
			
			if(stm.executeUpdate() < 1)
				throw new Exception("Khong update slideshow duoc !");
		}catch(Exception e){
			e.printStackTrace();
			result=0;
		}finally{
			if(getConn() != null)
				getConn().close();
		}		
		return result;
	}

	public int insertSlideshow(String title,String content,String image) throws SQLException{
		int result = 1;
		try{
			setConn(Util.Connect());
			String sql = "";
			sql = "INSERT INTO "+getTableName()+" (title,content,image,create_date) VALUES (?,?,?,?)";				

			PreparedStatement stm = getConn().prepareStatement(sql);
			
			Date currentDate = new Date();

			//gan gia tri cho cau sql
			stm.setString(1, title);
			stm.setString(2, content);
			stm.setString(3, image);
			stm.setInt(4, (int)(currentDate.getTime()/ 1000));

			if(stm.executeUpdate() < 1)
				throw new Exception("Khong insert slideshow duoc !");
		}catch(Exception e){
			e.printStackTrace();
			result=0;
		}finally{
			if(getConn() != null)
				getConn().close();
		}		
		return result;
	}			
	
	/**
	 * @param conn the conn to set
	 */
	public void setConn(Connection conn) {
		this.conn = conn;
	}

	/**
	 * @return the conn
	 */
	public Connection getConn() {
		return conn;
	}

	/**
	 * @param tableName the tableName to set
	 */
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	/**
	 * @return the tableName
	 */
	public String getTableName() {
		return tableName;
	}
}
